package com.example.administrator.kaohedemo.Adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.kaohedemo.Action.Actions;
import com.example.administrator.kaohedemo.Action.CourseListAction;
import com.example.administrator.kaohedemo.Model.CourseListBean;
import com.example.administrator.kaohedemo.R;
import com.example.administrator.kaohedemo.Utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/22.
 */
public class CourseListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private final CourseListAction courseListAction;
    private Context context;
    private List<CourseListBean.Mbody.Mmodules.Mcourses> list=new ArrayList<CourseListBean.Mbody.Mmodules.Mcourses>();
    private static final int LOADMORE=2;

    public CourseListAdapter(Context context){
        this.context=context;
        courseListAction=CourseListAction.getInstense();
    }

    public void setList(List<CourseListBean.Mbody.Mmodules> ll,int type){
        if (type==0){
            //刷新
            list.clear();
        }else {
            //加载更多
        }
        for (int i=0;i<ll.size();i++){
            List<CourseListBean.Mbody.Mmodules.Mcourses> mDatas=ll.get(i).courses;
            list.addAll(mDatas);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position==list.size()){
            return LOADMORE;
        }else {
            return super.getItemViewType(position);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType==LOADMORE){
            View view=LayoutInflater.from(context).inflate(R.layout.adapter_loadmore, parent, false);;
            return new LoadMoreViewHolder(view);
        }else {
            View view = LayoutInflater.from(context).inflate(R.layout.adapter_courselist, parent, false);
            return new MyViewHodler(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position<list.size()) {
            CourseListBean.Mbody.Mmodules.Mcourses bean = list.get(position);
            ((MyViewHodler)holder).classTitle.setText(bean.course_title);
            if (bean.record.equals("0")){
                ((MyViewHodler)holder).classContent.setTextColor(ContextCompat.getColor(context,R.color.color_000000));
                ((MyViewHodler) holder).classContent.setText("未观看");
            }else {
                ((MyViewHodler)holder).classContent.setTextColor(ContextCompat.getColor(context,R.color.color_999999));
                ((MyViewHodler) holder).classContent.setText("已观看:" + Utils.getTimeFromSum(bean.record));
            }
        }
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size()+1;
    }

    class LoadMoreViewHolder extends RecyclerView.ViewHolder{

        public LoadMoreViewHolder(View itemView) {
            super(itemView);
        }
    }

    class MyViewHodler extends RecyclerView.ViewHolder{

        ImageView classItemImg;
        TextView classTitle;
        TextView classContent;
        ImageView imgFlag;

        public MyViewHodler(View itemView) {
            super(itemView);
            classItemImg= (ImageView) itemView.findViewById(R.id.classItemImg);
            classTitle= (TextView) itemView.findViewById(R.id.classTitle);
            classContent= (TextView) itemView.findViewById(R.id.classContent);
            imgFlag= (ImageView) itemView.findViewById(R.id.imgFlag);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    courseListAction.SendItemClick(Actions.CourseListActions.TYPE_ITEM_ONCLICK,getLayoutPosition());
                }
            });
        }
    }
}
